package com.wjp.villagemp.controller.manage;

import com.wjp.villagemp.model.manage.ScoreEntity;
import com.wjp.villagemp.model.sys.PageResult;
import com.wjp.villagemp.service.manage.ScoreService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分数管理
 * @author lixr
 */
@RestController
@RequestMapping("/score")
public class ScoreController {
    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private ScoreService scoreService;

    /**
     * 分页查询所有分数信息
     * @param pageSize
     * @param page
     * @param studentNum
     * @param courseNum
     * @return
     */
    @GetMapping
    public PageResult scoreList(int pageSize, int page,@RequestParam(required = false, defaultValue = "0") int studentNum, @RequestParam(required = false, defaultValue = "0") int courseNum) {
        PageResult pageResult = new PageResult();
        try {
            List<Map<String, Object>> listScore = scoreService.listScore(pageSize, page * pageSize, studentNum, courseNum);
            pageResult.setData(listScore);
            Integer scoreCount = scoreService.countScore(studentNum, courseNum);
            pageResult.setTotalCount(scoreCount);
        } catch (Exception e) {
            log.error("分页查询所有分数信息异常：" + e.getMessage());
        }
        return pageResult;
    }


    /**
     * 新增分数信息
     * @param scoreEntity
     * @return
     */
    @PostMapping
    public Map<String, Object> insertScore(@RequestBody ScoreEntity scoreEntity) {
        Map<String, Object> resMap = new HashMap<>(16);
        try {
            if (scoreEntity != null) {
                Integer key = scoreService.insertScore(scoreEntity);
                if (key > 0) {
                    resMap.put("msg", "success");
                } else {
                    resMap.put("msg", "fail");
                }
            }
        } catch (Exception e) {
            log.error("新增分数信息异常：" + e.getMessage());
        }
        return resMap;
    }

    /**
     * 更新分数信息
     * @param scoreEntity
     * @return
     */
    @PutMapping
    public Map<String, Object> updateScore(@RequestBody ScoreEntity scoreEntity) {
        Map<String, Object> resMap = new HashMap<>(16);
        try {
            if (scoreEntity != null) {
                Integer key = scoreService.updateScore(scoreEntity);
                if (key > 0) {
                    resMap.put("msg", "success");
                } else {
                    resMap.put("msg", "fail");
                }
            }
        } catch (Exception e) {
            log.error("更新分数信息异常：" + e.getMessage());
        }
        return resMap;
    }

    /**
     * 批量删除分数信息
     * @param groupId
     * @return
     */
    @DeleteMapping
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> removeScore(@RequestBody List<Integer> groupId) {
        Map<String, Object> resMap = new HashMap<>(16);
        try {
            if (groupId.size() >= 1) {
                boolean flag = false;
                for (Integer scoreId : groupId) {
                    Integer key = scoreService.removeScore(scoreId);
                    if (key > 0) {
                        flag = true;
                    } else {
                        flag = false;
                    }
                }
                if (flag) {
                    resMap.put("msg", "success");
                }
            }
        } catch (Exception e) {
            log.error("批量删除分数信息异常：" + e.getMessage());
        }
        return resMap;
    }
}
